//
//  DDContactAcceptCell.m
//  DingDongZP
//
//  Created by Ray on 16/4/27.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDContactAcceptCell.h"
#import "DDContactRequestMessage.h"
#import "DDCustomLabel.h"

@interface DDContactAcceptCell ()

@property (nonatomic, strong) DDCustomLabel *phoneLab;

@end

@implementation DDContactAcceptCell
{
    UIImageView *backgroundImage;
    NSString *content;   //得到对方的微信号或者电话号码
    UIImageView *headerImage; //头像
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
       
        
        /**
         *  发送方
         *  点击同意交换电话/微信后   自己发送的消息内容
         */
        
       headerImage = (UIImageView *)self.portraitImageView;
        
       backgroundImage = [[UIImageView alloc] init];
       backgroundImage.userInteractionEnabled = YES;
       backgroundImage.image = [UIImage imageNamed:@"lALOZ3ab_0jNAdY.png"];
       backgroundImage.contentMode = UIViewContentModeScaleToFill;
       [self.contentView addSubview:backgroundImage];
        
        [backgroundImage mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(headerImage.mas_top).offset(5);
            make.right.equalTo(headerImage.mas_left).offset(-10);
            make.height.mas_equalTo(38);
            make.width.mas_equalTo(250);
        }];
        
        self.phoneLab = [[DDCustomLabel alloc] initWithFrame:CGRectMake(10, 10, 240, 20)];
        self.phoneLab.font = [UIFont systemFontOfSize:14.0];
        self.phoneLab.textAlignment = 1;
        self.phoneLab.textColor = COLOR_FONT_DARK_GRAY;
        [backgroundImage addSubview:self.phoneLab];
        
        
        /// 加入点事件处理
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        [backgroundImage addGestureRecognizer:tap];
    }
    return self;
}

- (void)setDataModel:(RCMessageModel *)model
{
    [super setDataModel:model];
    if ([model.content isKindOfClass:[DDContactRequestMessage class]])
    {
        DDContactRequestMessage *message = (DDContactRequestMessage*)model.content;
        NSString *typename = @"";
        NSString *username = @"";
        content = nil;
        if (message.requestType == DDContactRequestPhone) {
            typename = @"手机号码";
        }
        else {
            typename = @"微信号码";
        }
        
        /**
         *
         *  显示的名字为对方的名字
         */
        NSUserDefaults *theOppositeSide = [NSUserDefaults standardUserDefaults];
        NSString *userNameUserDefaults = [theOppositeSide objectForKey:@"theOppositeSide"];
        username = !userNameUserDefaults?@"对方":userNameUserDefaults;
        
        content = message.requestContent;
        
        self.phoneLab.text = [NSString stringWithFormat:@"%@的%@ : %@", username,typename, content];
        [self.phoneLab setAttributeLabelWithtextColorPairs:@[@{content:[UIColor colorWithRed:0.188 green:0.682 blue:0.678 alpha:1.000]}]];
    }

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.messageTimeLabel.hidden = YES;
    [self.nicknameLabel setHidden:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)actionTap:(id)sender
{
    DDContactRequestMessage *message = (DDContactRequestMessage *)self.model.content;
    if ([message isKindOfClass:[DDContactRequestMessage class]])
    {
        if (message.requestType == DDContactRequestPhone) {
            if ([self.contactDelegate respondsToSelector:@selector(didClickPhone:)]) {
                [self.contactDelegate didClickPhone:content];
            }
        }
        else {
            if ([self.contactDelegate respondsToSelector:@selector(didClickWexin:)]) {
                [self.contactDelegate didClickWexin:content];
            }
        }
    }
}

@end
